<template>
  <div class="comment">
    <!-- 头部 -->
    <myheader title="精彩评论">
      <template #left>
        <van-icon name="arrow-left" />
      </template>
    </myheader>
    <!-- 评论列表 -->
    <div class="list">
      <div class="item" v-for="v in commentList" :key="v.id">
        <div class="head">
          <img :src="axios.defaults.baseURL + v.user.head_img" alt />
          <div>
            <p>{{ v.user.nickname }}</p>
            <span>时间</span>
          </div>
          <span @click="replyComment(v)">回复</span>
        </div>
        <!-- 在使用嵌套子组件的位置添加监听子组件的事件，把子组件（commentItem.vue）传递过来的评论对象传递给articleFooter这个组件中去，实现发表评论操作 -->
        <!-- 因为之前已经写好了父（comment.vue）传子(articleFooter.vue)方法，只是传递的对象不一样，所以复用就可以了 -->
        <commentItem
          v-if="v.parent"
          :parent="v.parent"
          @reply="replyComment"
        ></commentItem>
        <div class="text">{{ v.content }}</div>
      </div>
    </div>
    <!-- 底部 -->
    <articleFooter
      :post="article"
      @refresh="refresh"
      :commentObj="temp"
      @cancelReply="temp = null"
    ></articleFooter>
  </div>
</template>

<script>
import myheader from "@/components/myheader";
import { getPostComment, getPostDetail } from "@/apis/post.js";
import axios from "../../utils/request";
import commentItem from "@/components/commentItem";
import articleFooter from "../../components/articleFooter";
export default {
  data() {
    return {
      commentList: [],
      axios,
      article: {},
      temp: {},
    };
  },
  components: {
    myheader,
    commentItem,
    articleFooter,
  },
  async mounted() {
    this.init();
  },
  methods: {
    async init() {
      let id = this.$route.params.id;
      // 获取评论列表
      let res = await getPostComment(id);
      console.log(res);
      this.commentList = res.data.data;
      console.log(this.commentList);

      // 获取文章详情
      this.article = (await getPostDetail(id)).data.data;
    },
    refresh() {
      this.init();
      //   发布成功，移动到最上面
      window.scrollTo(0, 0);
    },
    // 点击回复，传评论对象到子组件
    replyComment(v) {
      // 因为遍历的值不在 子组件可以传值的范围，所以需要第三方变量，用来传值给子组件
      this.temp = v;
      console.log("父组件", this.temp);
    },
  },
};
</script>

<style lang="less" scoped>
.list {
  padding-bottom: 60px;
}
.item {
  padding: 10px;
  border-bottom: 1px solid #ccc;
  .head {
    display: flex;
    justify-content: space-between;
    align-items: center;
    > img {
      width: 50/360 * 100vw;
      height: 50/360 * 100vw;
      display: block;
      border-radius: 50%;
    }
    > div {
      flex: 1;
      display: flex;
      flex-direction: column;
      margin-left: 10px;
      > span {
        font-size: 12px;
        color: #999;
        line-height: 25px;
      }
    }
    > span {
      color: #999;
      font-size: 13px;
    }
  }
  .text {
    font-size: 14px;
    color: #333;
    padding: 20px 0 10px 0;
  }
}
</style>